package com.jsonal.controller.manager;

import com.jsonal.controller.BaseController;
import com.jsonal.entity.api.UserBean;
import com.jsonal.form.user.UserForm;
import com.jsonal.service.api.UserService;
import com.jsonal.vo.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author: jingji.lin
 * @Description: 用户管理
 * @Date: 2020/6/17 8:40
 * @Version: 1.0
 */
@Controller
public class UserManagerController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/managers/getUserList", method = RequestMethod.GET)
    @ApiOperation(value = "/managers/getUserList", httpMethod = "GET", notes = "分页获取用户列表")
    @ResponseBody
    public Result getUserList(UserForm userForm) {
        UserBean userBean = new UserBean();
        this.setFormToBean3(userForm, userBean);
        return userService.getUserList(userBean);
    }

    @RequestMapping(value = "/managers/changeState/{id}/{state}", method = RequestMethod.POST)
    @ApiOperation(value = "/managers/changeState/{id}/{state}", httpMethod = "POST", notes = "修改用户状态")
    @ResponseBody
    public Result changeState(@PathVariable String id, @PathVariable Boolean state) {
        if (id == null || "".equals(id)) {
            return new Result(false, "id不能为空");
        }
        return userService.changeState(id, state);
    }

    @RequestMapping(value = "/managers/addOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "/managers/addOrUpdate", httpMethod = "POST", notes = "添加或修改用户")
    @ResponseBody
    public Result addOrUpdate(@RequestBody UserForm userForm) {
        UserBean userBean = new UserBean();
        this.setFormToBean3(userForm, userBean);
        return userService.addOrUpdate(userBean);
    }

    @RequestMapping(value = "/managers/getUserById/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "/managers/getUserById/{id}", httpMethod = "GET", notes = "根据用户 id 获取信息")
    @ResponseBody
    public Result getUserById(@PathVariable String id) {
        if (id == null || "".equals(id)) {
            return new Result(false, "id不能为空");
        }
        return userService.getUserById(id);
    }

    @RequestMapping(value = "/managers/removeUser/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "/managers/removeUser/{id}", httpMethod = "POST", notes = "根据用户 id 删除用户")
    @ResponseBody
    public Result removeUser(@PathVariable String id) {
        if (id == null || "".equals(id)) {
            return new Result(false, "id不能为空");
        }
        return userService.removeUser(id);
    }
}
